// 03myCenter/02sectorization/02myOrder/myOrder.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var _this

Page({

    /*页面的初始数据*/
    data: {
        orderInfo: [],
        // 订单状态
        orderStatus: [],
        // 商品数量
        productsNumber: [],
        // 商品名称
        productsName: [],
        //再来一单
        onceAgian: [],
    },

    /*生命周期函数--监听页面加载*/
    onLoad: function (options) {
        _this = this
        
    },
    // 返回进入时刷新页面
    onShow: function(){
        this.getMyOrderInfo()
    },
    // 获取订单信息
    getMyOrderInfo: function () {
        var param = {
            User_ID: app.Data.userInfo.User_ID
        }
        var os = this.data.orderStatus
        var pn = this.data.productsNumber
        var pa = this.data.productsName
        var oc = this.data.onceAgian
        co.getShareApi('order/Omg/getList', param, function (res) {
            // console.log('【获取返回信息】===',res.data)
            if (res.code == 0) {
                var re = res.data
                var statusName
                var onceAgian
                // 存入订单状态
                for (var i = 0; i < re.length; i++) {
                    var status = re[i].Order_Status
                    var count = re[i].Order_CartList.length
                    var name = re[i].Order_CartList[0].ProductsName
                    if (status == 1) {
                        statusName = '订单等待支付'
                        onceAgian = '去支付'
                    } else if (status == 2) {
                        statusName = '订单已支付'
                        onceAgian = '再来一单'
                    } else if (status == 3) {
                        statusName = '等待确认收货'
                        onceAgian = '确认收货'
                    } else if (status == 4) {
                        statusName = '订单已完成'
                        onceAgian = '查看订单'
                    } else if (status == 5) {
                        statusName = '订单已取消'
                        onceAgian = '退款详情'
                    }
                    pa.push(name)
                    pn.push(count)
                    os.push(statusName)
                    oc.push(onceAgian)
                }
                _this.setData({
                    orderInfo: re,
                    orderStatus: os,
                    productsNumber: pn,
                    productsName: pa,
                    onceAgian: oc
                })
            }
        })
        // console.log('【获取订单状态数组】===', this.data.orderStatus)
    },
    // 进入订单详情
    intoOrderDetails: function (e) {
        var index = e.currentTarget.id
        var obj = JSON.stringify(this.data.orderInfo[index])
        wx.navigateTo({
            url: '/03myCenter/02sectorization/02myOrder/orderDetails/orderDetails?obj=' + obj,
        })
    },
    // 进入商店首页
    intoShoHome: function (e) {
        wx.navigateTo({
            url: '/02local/02shopHome/shopHome?Biz_ID=' + e.currentTarget.id,
        })
    },
    // 点击支付或查看
    check: function (e) {
        var name = e.currentTarget.id
        if (name == '查看订单') {
            var e = {
                currentTarget: {
                    id: e.currentTarget.dataset.index
                }
            }
            this.intoOrderDetails(e)
        } else if (name == '去支付') {
            //购买分销商金额
            var param = {
                openid: app.Data.userInfo.openid,
                appid: app.Data.app.appid,
                key: 'sUiYuAnTeChNoLoGyHtTpSwWwLs293cC',
                notify_url: 'http://api.igoyp.com/public/weixinpay/notify.php',
                mch_id: '1480163852',
                money: 0.01,
                abstract: '买单'
            }
            co.getShareApi('api/Wxpay/createParam', param, function (res) {
                res.data.success = function (result) {
                    _this.upDataOrderStatus()
                    console.log('【调起支付成功res】', result)
                }
                res.data.fail = function (result) {
                    console.log('【调起支付失败res】', result)
                }
                //调起微信支付
                wx.requestPayment(res.data)
            })
        }
    },
    // 更改订单状态
    upDataOrderStatus: function () {
        var param = {
            Order_ID: app.Data.Order_ID,
            Order_Status: 4
        }
        co.getShareApi('order/Omg/updateMealOrder', param, function (res) {
            console.log("【获取返回信息】===", res.msg)
            if(res.code == 0){
                this.getMyOrderInfo() 
            }
        })
    }
})